const cheerio = require("cheerio")
const axios = require("axios")
const fs = require('fs')
    // const url = require('url')
const path = require('path')
    // 获取html文档的内容


let httpUrl = "https://www.doutula.com/article/list/?page=1"

axios.get(httpUrl).then((res) => {
    // console.log(res.data)
    // cheerio解析html文档
    let $ = cheerio.load(res.data)
    $('#home .col-sm-9>a').each((i, ele) => {
        // 获取当前页面所有表情页面的链接
        // console.log(ele);
        // console.log($(ele).attr('href'))
        let pageUrl = $(ele).attr('href');
        let title = $(ele).find('.random_title').text();
        let reg = /(.*?)\d/igs;
        title = reg.exec(title)[1];
        fs.mkdir('./img/' + title, function(err) {
            if (err) {
                // console.log(err);
            } else {
                console.log("图片路径创建成功" + './img/' + title)
            }
        });
        // console.log(title)
        parsePage(pageUrl, title)

    })

    // 解析页面
    async function parsePage(url, title) {
        // 获取每一个链接的详情页
        let res = await axios.get(url);
        let $ = cheerio.load(res.data);
        $('.pic-content img').each((i, ele) => {
            let imgUrl = $(ele).attr('src');
            // console.log(imgUrl);
            // 

            // console.log(path.parse(imgUrl));
            // 图片写入的路径和名字

            let extName = path.extname(imgUrl)
            let imgPath = `img/${title}/${title}-${i}${extName}`;
            // 创建写入流
            let ws = fs.createWriteStream(imgPath);
            axios.get(imgUrl, { responseType: 'stream' }).then(function(res) {
                res.data.pipe(ws)
                console.log('图片加载完成:' + imgPath)
            })
        })

    }

})